package com.cxyzy.basic_libs

import android.app.Activity
import android.os.Build
import android.webkit.WebSettings
import android.webkit.WebView

/**
 * 浏览网页
 */
fun Activity.打开网页(url: String) = browse(url)

fun Activity.browse(url: String) = openBrowser(this, url)

private fun openBrowser(activity: Activity, url: String) {
    val webView = WebView(activity)
    addView(webView, activity)
    val settings = webView.settings
    settings.javaScriptEnabled = true//设置WebView属性，能够执行Javascript脚本
    settings.cacheMode = WebSettings.LOAD_NO_CACHE
    settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
    settings.allowFileAccess = true //设置可以访问文件
    settings.builtInZoomControls = false //设置支持缩放
    settings.setSupportZoom(true)
    settings.useWideViewPort = true
    settings.loadWithOverviewMode = true
    settings.setAppCacheEnabled(true)
    settings.domStorageEnabled = true
    settings.databaseEnabled = true
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
    }
    webView.loadUrl(url)
}